<?php

class Application_Model_CalendarMapper
{
 protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Calendar');
        }
        return $this->_dbTable;
    }

    public function insert(Application_Model_Calendar $calendar)
    {
    try{
        $data = array(
            'START_DATE'=>$calendar->getStartDate(),
            'START_TIME'=>$calendar->getStartTime(),
            'END_DATE'=>$calendar->getEndDate(),
            'END_TIME'=>$calendar->getEndTime(),
            'DESCRIPTION'=>$calendar->getDescription(),
            'CREATED_DATE'=>$calendar->getCreatedDate(),
            'CREATED_BY'=>$calendar->getCreatedBy(),
            'FOREGROUND'=>$calendar->getForeground(),
            'BACKGROUND'=>$calendar->getBackground()
        );

         $eventId = $this->getDbTable()->insert($data);

         return $eventId;

    }catch(Zend_Db_Statement_Exception $e){
        Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
          throw new Exception("Technical difficulties experienced in adding Calendar");
    }
 }
 
     public function update(Application_Model_Calendar $calendar)
    {
    try{
        $data = array(
            'START_DATE'=>$calendar->getStartDate(),
            'START_TIME'=>$calendar->getStartTime(),
            'END_DATE'=>$calendar->getEndDate(),
            'END_TIME'=>$calendar->getEndTime(),
            'DESCRIPTION'=>$calendar->getDescription(),
            'UPDATED_DATE'=>$calendar->getCreatedDate(),
            'UPDATED_BY'=>$calendar->getCreatedBy(),
            'FOREGROUND'=>$calendar->getForeground(),
            'BACKGROUND'=>$calendar->getBackground()
        );

        $this->getDbTable()->update($data, array('EVENT_ID = ?' => $calendar->getEventId()));

    }catch(Zend_Db_Statement_Exception $e){
        Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
          throw new Exception("Technical difficulties experienced in updating Calendar");
    }
 }

  public function get( $dt)
    {
         try{
             $month= date('m',strtotime($dt));
             $year= date('Y',strtotime($dt));


           $select =  $this->getDbTable()->select();

           $select->where('MONTH(START_DATE) = ? ', $month);
           $select->where('YEAR(START_DATE) = ? ', $year);
           $select->where('MONTH(END_DATE) = ? ', $month);
           $select->where('YEAR(END_DATE) = ? ', $year);


            $rows =  $this->getDbTable()->fetchAll($select);

            return $rows;

         }catch(Zend_Db_Statement_Exception $e){
             Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
             throw new Exception("Technical difficulties experienced while fetching Calendar");
          }

    }

    public function deleteAgenda($id){
         try{

           $where=  $this->getDbTable()->getAdapter()->quoteInto('EVENT_ID=?', $id);

          $this->getDbTable()->delete($where);


         }catch(Zend_Db_Statement_Exception $e){
             Zend_Registry::get('logger')->setEventItem('class', __CLASS__)->err($e->getMessage());
             throw new Exception("Technical difficulties experienced while deleting Calendar");
          }
    }

}

